$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'offer/offer/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '上游对应的渠道id 默认89', name: 'dspChannelId', index: 'dsp_channel_id', width: 80 }, 			
			{ label: '', name: 'sspChannelId', index: 'ssp_channel_id', width: 80 }, 			
			{ label: '上游offerid', name: 'dspOfferId', index: 'dsp_offer_id', width: 80 }, 			
			{ label: '下游offerid', name: 'sspOfferId', index: 'ssp_offer_id', width: 80 }, 			
			{ label: '物料名称', name: 'name', index: 'name', width: 80 }, 			
			{ label: '状态 active', name: 'status', index: 'status', width: 80 }, 			
			{ label: '广告类型', name: 'category', index: 'category', width: 80 }, 			
			{ label: '否手头可以手动进行投放 默认1', name: 'offerApproval', index: 'offer_approval', width: 80 }, 			
			{ label: '', name: 'offerApprovalMsg', index: 'offer_approval_msg', width: 80 }, 			
			{ label: '追踪url', name: 'trackingLink', index: 'tracking_link', width: 80 }, 			
			{ label: '投放结束时间', name: 'endDate', index: 'end_date', width: 80 }, 			
			{ label: '广告结算类型CPA · CPC · CPS · CPA+CPS', name: 'pricingType', index: 'pricing_type', width: 80 }, 			
			{ label: '成交价格', name: 'payout', index: 'payout', width: 80 }, 			
			{ label: '支付百分比', name: 'percentPayout', index: 'percent_payout', width: 80 }, 			
			{ label: '', name: 'currency', index: 'currency', width: 80 }, 			
			{ label: '推广网址', name: 'previewUrl', index: 'preview_url', width: 80 }, 			
			{ label: '转换跟踪方法用于报价', name: 'conversionProtoco', index: 'conversion_protoco', width: 80 }, 			
			{ label: '', name: 'conversionProtocolMsg', index: 'conversion_protocol_msg', width: 80 }, 			
			{ label: '提供的链接地址的缩略图', name: 'thumbnail', index: 'thumbnail', width: 80 }, 			
			{ label: '具体投放的地区 直接存json就行了', name: 'offerGeo', index: 'offer_geo', width: 80 }, 			
			{ label: '', name: 'offerVbt', index: 'offer_vbt', width: 80 }, 			
			{ label: '具体结算的类型 直接存json就行了', name: 'offerEvent', index: 'offer_event', width: 80 }, 			
			{ label: '', name: 'offerPostback', index: 'offer_postback', width: 80 }, 			
			{ label: '具体投放平台 直接存json', name: 'offerPlatform', index: 'offer_platform', width: 80 }, 			
			{ label: '', name: 'offerCreative', index: 'offer_creative', width: 80 }, 			
			{ label: '预算上限  直接存json', name: 'offerCap', index: 'offer_cap', width: 80 }, 			
			{ label: '', name: 'createTime', index: 'create_time', width: 80 }, 			
			{ label: '', name: 'updateTime', index: 'update_time', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		offer: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.offer = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.offer.id == null ? "offer/offer/save" : "offer/offer/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.offer),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "offer/offer/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "offer/offer/info/"+id, function(r){
                vm.offer = r.offer;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});